<template>
	<view class="contain">
		<view class="header">
			<u-avatar :src="avatar" size="150rpx" class="avatar" @click="changeAvatar"></u-avatar>
			<view class="name">
				{{name}}
			</view>
		</view>

		<view class="iform">
			<view class="title">
				修改账户信息
			</view>
			<u--form ref="uForm">
				<u-form-item label="密码" prop="name">
					<u-input placeholder="修改密码" type="password" v-model="password" />
				</u-form-item>
				<u-form-item label="邮箱" prop="name">
					<u-input placeholder="修改邮箱" v-model="email" />
				</u-form-item>
				<view class="l">

				</view>
				<u-button text="提交" @click="submit"></u-button>
			</u--form>
		</view>


	</view>
</template>

<script>
	import api from '@/api/index.js'; // 引入api

	export default {
		data() {
			return {
				password: "",
				email: "",
				avatar: "",
				name: ""
			};
		},
		onLoad(option) {
			// console.log(option)
			uni.setStorageSync("id", option.id)
			this.name = option.name
			this.email = option.email
			this.avatar = option.avatar
		},
		destroyed() {
			uni.removeStorageSync("id")
		},
		methods: {
			submit() {
				if (this.email === "" && this.password === "") {
					return
				}
				let id = uni.getStorageSync("id")
				let token = uni.getStorageSync("token")
				let data = {

				}

				if (this.email !== "") {
					data.email = this.email
				}
				if (this.password !== "") {
					data.password = this.password
				}
				api.changeInfo(token, id, data).then(res => {
					console.log(res)
					if (res.code == 1) {

						uni.showModal({
							content: '修改成功',
							showCancel: false
						});
						uni.navigateBack()

					} else if (res.code == 0) {
						uni.showModal({
							content: res.errors,
							showCancel: false
						});
					}
				}).catch(err => {
					console.log(err)
				});

			},
			changeAvatar() {
				let token = uni.getStorageSync("token")
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择

					success: (res) => {
						this.avatar = res.tempFilePaths[0]
						uni.uploadFile({
							url: 'http://127.0.0.1:8000/avatar/', //仅为示例，非真实的接口地址
							header: {
								'Authorization': 'JWT ' + token // 参数
							},
							filePath: res.tempFilePaths[0],
							name: 'avatar',
							success: (res) => {
								let data = JSON.parse(res.data)
								console.log(data)
								if (data.code == 0) {
									uni.showModal({
										content: "上传失败",
										showCancel: false
									})
								}

							}
						});
					}
				});
			}
		}

	}
</script>

<style lang="less">
	.contain {
		.header {
			height: 150px;
			background-color: #EEEEEE;
			display: flex;
			flex-direction: row;
			justify-content: space-around;
			align-items: center;

			.name {
				width: 300rpx;
				height: 100rpx;
				line-height: 100rpx;
				font-size: 40rpx;
				// color: white;
			}
		}

		.iform {
			padding: 20px;

			.title {
				height: 120rpx;
				line-height: 120rpx;
				font-size: 50rpx;
			}

			.l {
				height: 80rpx;
			}
		}

	}
</style>
